package com.example.company.controller;


import com.example.company.entity.ApiResult;
import com.example.company.entity.ApiResultHandler;
import com.example.company.entity.Departments;
import com.example.company.entity.Positions;

import com.example.company.mapper.PositionsMapper;
import com.example.company.service.IPositionsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 存储职位信息 前端控制器
 * </p>
 *
 * @author 88Ra
 * @since 2024-12-31
 */
@RestController
@RequestMapping("/api/system/positions")
public class PositionsController {
	@Autowired
	private IPositionsService iPositionsService;
	
	@Autowired
	private PositionsMapper positionsMapper;
	
	@GetMapping("/list")
	public PageInfo<Positions> findPage(@RequestParam(defaultValue = "1") Integer pageNum
			, @RequestParam(defaultValue = "10") Integer pageSize, Positions positions) {
		
		PageInfo<Positions> pageInfo=iPositionsService.findPage(pageNum, pageSize, positions);
		return pageInfo;
	}
	
	@PostMapping()
	public ApiResult addpositin (@RequestBody Positions positions){
		int count =positionsMapper.insert(positions);
		if (count == 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", positions);
			
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	
	@DeleteMapping("/{positionids}")
	public ApiResult deleteusers(@PathVariable Long[] positionids) {
		
		int cur = positionsMapper.deletePositionsByPositionIDs(positionids);
		
		if (cur >= 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", cur);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	@CrossOrigin
	@GetMapping("/lists")
	public ApiResult findpositions() {
		List<Positions> positions = new ArrayList<>();
		positions = positionsMapper.selectlist();
		if (!positions.isEmpty()) {
			System.out.println(positions);
			return ApiResultHandler.buildApiResult(200, "请求成功", positions);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
		
	}
	
	
	
}
